package com.wd.health.view.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;

import com.wd.health.R;
import com.wd.health.view.fragment.StartFragment01;
import com.wd.health.view.fragment.StartFragment02;
import com.wd.health.view.fragment.StartFragment03;
import com.wd.health.view.fragment.StartFragment04;
import com.wd.health.view.fragment.StartFragment05;
import com.google.android.material.tabs.TabLayout;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

public class StartActivity extends AppCompatActivity {

    private ViewPager vp;
    private TabLayout tl;
    private List<String> slist = new ArrayList<>();
    private List<Fragment> flist = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        ImmersionBar.with(this).init();
        initView();
        initData();
    }

    private void initData() {
        //添加tablayout数据
        slist.add("");
        slist.add("");
        slist.add("");
        slist.add("");
        slist.add("");
        //添加fragment数据
        flist.add(new StartFragment01());
        flist.add(new StartFragment02());
        flist.add(new StartFragment03());
        flist.add(new StartFragment04());
        flist.add(new StartFragment05());
        //设置适配器
        vp.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        //联动
        tl.setupWithViewPager(vp);
    }

    private void initView() {
        vp = findViewById(R.id.vp_start);
        tl = findViewById(R.id.tl_start);
    }
    //viewPager适配器
    class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return flist.get(position);
        }

        @Override
        public int getCount() {
            return flist.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return slist.get(position);
        }
    }
}